#define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[]) { int s, j; cpu_set_t cpuset; pthread_t thread;
thread = pthread_self(); // 获取当前运行的线程数据结构
/* Set affinity mask to include CPUs 0 to 7 */ CPU_ZERO(&cpuset); // 将cpuset结构清零 // 将0-7号CPU全部加入cpuset中(如果只想指定特定的cpu,运行循环中那一行即可) // 例如将线程运行在3号cpu上:CPU_SET(3, &cpuset); for (j = 0; j < 8; j++) CPU_SET(j, &cpuset); // 设置j号cpu到cpuset中。
// set CPU affinity of a thread s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np");
/* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np");
printf("Set returned by pthread_getaffinity_np() contained:\n"); for (j = 0; j < CPU_SETSIZE; j++) if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j);
exit(EXIT_SUCCESS); }
如果gcc无法通过编译:undefined reference to pthread_setaffinity_np